package com.zatech.cgnci.project.controller.claim;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zatech.cgnci.project.base.context.UserContext;
import com.zatech.cgnci.project.base.utils.VerifyUtils;
import com.zatech.cgnci.project.dto.CgnciClaimCaseQueryDTO;
import com.zatech.cgnci.project.dto.ClaimCaseTaskQueryOptionDTO;
import com.zatech.cgnci.project.entity.UserEntity;
import com.zatech.cgnci.project.service.CgnciClaimAlertInfoService;
import com.zatech.cgnci.project.service.CgnciClaimCaseService;
import com.zatech.cgnci.project.service.CgnciClaimCaseTaskService;
import com.zatech.cgnci.project.service.CgnciOperationLogsService;
import com.zatech.cgnci.project.vo.CgnciClaimCasePageWorkbenchVO;
import com.zatech.cgnci.project.vo.CgnciClaimCaseVO;
import com.zatech.cgnci.project.vo.CgnciClaimReopenVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/api/claim/queryTask")
@RequiredArgsConstructor
@Api(produces = "application/json", tags = "理赔查询")
public class CgnciClaimQueryTaskController {

    private final CgnciClaimCaseTaskService cgnciClaimCaseTaskService;
    private final CgnciClaimCaseService cgnciClaimCaseService;
    private final CgnciOperationLogsService cgnciOperationLogsService;
    private final CgnciClaimAlertInfoService cgnciClaimAlertInfoService;

    @PostMapping("/caseDetailTask")
    @ApiOperation(value = "查询案件详情-任务")
    public CgnciClaimCaseVO caseDetailTask(@RequestBody ClaimCaseTaskQueryOptionDTO<String> req) {
        if (Objects.isNull(req.getTaskNo())) return null;
        return cgnciClaimCaseTaskService.caseDetailTask(req);
    }

    @PostMapping("/casePageTask")
    @ApiOperation(value = "案件分页查询-任务")
    public IPage<CgnciClaimCasePageWorkbenchVO> casePageTask(@RequestBody CgnciClaimCaseQueryDTO req) {
        // 获取当前登录用户
        UserEntity user = UserContext.getUser();
        IPage<CgnciClaimCasePageWorkbenchVO> page = cgnciClaimCaseTaskService.casePageTaskWorkbench(req);
        List<CgnciClaimCasePageWorkbenchVO> records = page.getRecords();
        if (VerifyUtils.isNotEmpty(records)) {
            records.forEach(record -> record.setHasEdit(VerifyUtils.isNotEmpty(record.getCurrentApproverAndName()) && record.getCurrentApproverAndName().contains(user.getNameAndCode())));
        }
        return page;
    }

    @GetMapping("/reopenHistory")
    @ApiOperation(value = "查询重开历史")
    public List<CgnciClaimReopenVO> reopenHistory(@RequestParam(value = "caseNo") String caseNo) {
        return cgnciClaimCaseTaskService.getReopenHistory(caseNo);
    }
}
